package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.CharItemsReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_AddItem;
import com.lineage.server.serverpackets.S_DeleteInventoryItem;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.utils.log.PlayerLogUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

public class Itemup {
  private static ArrayList aData17 = new ArrayList();
  
  private static Itemup _instance;
  
  private static boolean NO_MORE_GET_DATA17 = false;
  
  private static Random _random = new Random();
  
  public static final String TOKEN = ",";
  
  public static Itemup get() {
    if (_instance == null)
      _instance = new Itemup(); 
    return _instance;
  }
  
  public static void forresolvent(L1PcInstance pc, L1ItemInstance item, int l) {
    int itemid = item.getItemId();
    L1ItemInstance tgitem = pc.getInventory().getItem(l);
    ArrayList aTempData = null;
    int[] iTemp = null;
    if (!NO_MORE_GET_DATA17) {
      NO_MORE_GET_DATA17 = true;
      getData17();
    } 
    int i = 0;
    while (i < aData17.size()) {
      aTempData = (ArrayList<Integer>) aData17.get(i);
      if (((Integer)aTempData.get(0)).intValue() == itemid) {
        if (((Integer)aTempData.get(2)).intValue() != 0 && 
          pc.getLevel() < ((Integer)aTempData.get(2)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("等级" + ((Integer)aTempData.get(2)).intValue() + "以上才可使用此道具。"));
          return;
        } 
        if (((Integer)aTempData.get(0)).intValue() == item.getItemId() && (
          (Integer)aTempData.get(5)).intValue() == tgitem.getItemId()) {
          if (tgitem.getBless() >= 4) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("该装备受到封印卷轴的影响。"));
            return;
          } 
          if (pc.getDoll(tgitem.getId()) != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
            return;
          } 
          if (tgitem.isEquipped()) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("请先把装备脱掉在使用。"));
            return;
          } 
          if (!pc.getDolls().isEmpty()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("请先解除魔法娃娃。"));
            return;
          } 
          if (pc.getInventory().getSize() >= 170) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("身上物品太多无法进行升级"));
            return;
          } 
          if (pc.getInventory().getWeight() / pc.getMaxWeight() * 100.0D > 90.0D) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("身上负重限制无法进行升级"));
            return;
          } 
          int[] materials = (int[])aTempData.get(3);
          int[] counts = (int[])aTempData.get(4);
          boolean isCreate = true;
          int j = 0;
          while (j < materials.length) {
            if (!pc.getInventory().checkItem(materials[j], counts[j])) {
              L1Item temp = ItemTable.get().getTemplate(materials[j]);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp.getName())) + "(" + (
                    counts[j] - pc.getInventory().countItems(temp.getItemId())) + ")"));
              isCreate = false;
            } 
            j++;
          } 
          if (isCreate) {
            int k = 0;
            while (k < materials.length) {
              pc.getInventory().consumeItem(materials[k], counts[k]);
              k++;
            } 
            int ran = 0;
            if (pc.isGm()) {
              ran = 99;
            } else {
              ran = ((Integer)aTempData.get(6)).intValue();
            } 
            if (ran >= _random.nextInt(100) + 1) {
              L1Item l1item = ItemTable.get().getTemplate(((Integer)aTempData.get(7)).intValue());
              pc.sendPackets((ServerBasePacket)new S_DeleteInventoryItem(tgitem.getId()));
              tgitem.setItemId(((Integer)aTempData.get(7)).intValue());
              tgitem.setItem(l1item);
              tgitem.setBless(l1item.getBless());
              try {
                CharItemsReading.get().updateItemId_Name(tgitem);
              } catch (Exception e) {
                e.printStackTrace();
              } 
              pc.sendPackets((ServerBasePacket)new S_AddItem(tgitem));
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, tgitem.getLogName()));
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY" + aTempData.get(9)));
              升级成功("玩家 :" + pc.getName() + "使用 " + item.getName() + "对" + tgitem.getName() + "升级成功 ,时间:" + 
                  new Timestamp(System.currentTimeMillis()) + ")");
              isCreate = false;
              L1ItemInstance item2 = pc.getInventory()
                .findItemId(((Integer)aTempData.get(0)).intValue());
              pc.getInventory().removeItem(item2.getId(), 1L);
              break;
            } 
            isCreate = false;
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(""+aTempData.get(10)));
            if (((Integer)aTempData.get(11)).intValue() == 1)
              pc.getInventory().removeItem(tgitem.getId(), 1L); 
            L1ItemInstance item3 = pc.getInventory()
              .findItemId(((Integer)aTempData.get(0)).intValue());
            pc.getInventory().removeItem(item3.getId(), 1L);
            break;
          } 
        } 
      } 
      i++;
    } 
  }
  
  private static void getData17() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_道具升级系统");
      ArrayList aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("item")));
          aReturn.add(1, ""+ rset.getString("note"));
          aReturn.add(2, new Integer(rset.getInt("CheckLevel")));
          if (rset.getString("CheckItem") != null && !rset.getString("CheckItem").equals("") && 
            !rset.getString("CheckItem").equals("0")) {
            aReturn.add(3, getArray(rset.getString("CheckItem"), ",", 1));
          } else {
            aReturn.add(3, null);
          } 
          if (rset.getString("Checkcount") != null && !rset.getString("Checkcount").equals("") && 
            !rset.getString("Checkcount").equals("0")) {
            aReturn.add(4, getArray(rset.getString("Checkcount"), ",", 1));
          } else {
            aReturn.add(4, null);
          } 
          aReturn.add(5, new Integer(rset.getInt("ne_item")));
          aReturn.add(6, new Integer(rset.getInt("random")));
          aReturn.add(7, new Integer(rset.getInt("giveitem")));
          aReturn.add(8, new Integer(rset.getInt("givecount")));
          aReturn.add(9, rset.getString("Success"));
          aReturn.add(10, rset.getString("Fail"));
          aReturn.add(11, new Integer(rset.getInt("save_type")));
          aData17.add(aReturn);
        }  
      SQLUtil.close(rset);
      SQLUtil.close(stat);
      SQLUtil.close(con);
    } catch (Exception exception) {}
  }
  
  public static void 升级成功(String info) {
    PlayerLogUtil.writeLog("[升级成功]", info);
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
